package com.portal.admin.office.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.portal.admin.base.entity.BaseAction;
import com.portal.admin.base.entity.DateUtils;
import com.portal.admin.base.entity.ResultInfo;
import com.portal.admin.office.entity.SysOffice;
import com.portal.admin.office.service.SysOfficeService;
import com.portal.admin.utils.admin.StringUtils;
import com.portal.admin.utils.excel.ExportExcel;
import com.portal.admin.utils.excel.ImportExcel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
*【机构信息】页面接口
*/
@RestController
@RequestMapping("/admin/sysOffice")
public class SysOfficeController extends BaseAction {

    @Autowired
    private SysOfficeService sysOfficeService;

    /**
    * 【机构信息】根据条件分页查询
    * @param page
    * @param sysOffice
    * @return
    */
    @RequestMapping("/getPage")
    @RequiresPermissions("sysOffice:getPage")
    public ResultInfo getPage(Page<SysOffice> page, SysOffice sysOffice) {
        IPage<SysOffice> iPage = sysOfficeService.getPage(page, sysOffice);
        return ResultInfo.ok("获取分页成功", iPage);
    }

    /**
    * 【机构信息】根据条件查询
    * @param sysOffice
    * @return
    */
    @RequestMapping("/getList")
    public ResultInfo getList(SysOffice sysOffice) {
        List<SysOffice> list = sysOfficeService.getList(sysOffice);
        return ResultInfo.ok("获取列表成功", list);
    }

    /**
     * 【机构信息】根据id查询
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @RequiresPermissions({"sysOffice:edit","sysOffice:view"})
    public ResultInfo get(String id) {
        SysOffice sysOffice = sysOfficeService.get(id);
        return ResultInfo.ok("获取对象成功", sysOffice);
    }

    /**
     * 【机构信息】提交(新增或修改)
     * @param sysOffice
     * @return
     */
    @RequestMapping("/sub")
    @RequiresPermissions("sysOffice:save")
    public ResultInfo insert(SysOffice sysOffice) {
        if (StringUtils.isEmpty(sysOffice.getId())) { //新增
            sysOfficeService.insert(sysOffice);
        } else {//修改
            sysOfficeService.update(sysOffice);
        }
        return ResultInfo.ok("提交成功!");
    }

    /**
     * 获取机构树
     * @return
     */
    @RequestMapping("/getOfficeTree")
    public ResultInfo getOfficeTree(SysOffice sysOffice) {
        List<SysOffice> root = new ArrayList<>();
        List<SysOffice> list = sysOfficeService.getList(sysOffice);
        if(list.size()>0){
            root.add(list.get(0));
        }
        List<SysOffice> treeList = sysOfficeService.getOfficeChildren(root);
        return ResultInfo.ok("获取列表成功", treeList);
    }

    /**
    * 【机构信息】删除
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    @RequiresPermissions("sysOffice:delete")
    public ResultInfo delete(String id) {
        sysOfficeService.delete(id);
        SysOffice office = new SysOffice();
        office.setPid(id);
        List<SysOffice> list = sysOfficeService.getList(office);
        for (SysOffice sysOffice : list) {
            sysOfficeService.delete(sysOffice.getId());
        }
        return ResultInfo.ok("删除成功!");
    }

    /**
     *【机构信息】导出
     */
    @RequestMapping(value = "/export")
    @RequiresPermissions("sysOffice:export")
    public void exportFile(SysOffice sysOffice, HttpServletResponse response) {
        try {
            String fileName = "通知信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            List<SysOffice> list = sysOfficeService.getList(sysOffice);
            new ExportExcel("通知信息", SysOffice.class).setDataList(list).write(response, fileName).dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *【机构信息】导入
     */
    @RequestMapping(value = "/import")
    @RequiresPermissions("sysOffice:import")
    public ResultInfo importFile(MultipartFile file) {
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<SysOffice> list = ei.getDataList(SysOffice.class);
            for (SysOffice sysOffice : list) {
                try {
                    sysOfficeService.insert(sysOffice);
                    successNum++;
                } catch (Exception ex) {
                    failureNum++;
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条机构信息记录。");
            }
            return ResultInfo.ok("已成功导入 " + successNum + " 条机构信息记录" + failureMsg);
        } catch (Exception e) {
            return ResultInfo.error("导入机构信息失败！失败信息：" + e.getMessage());
        }
    }

    /**
     *【机构信息】模板下载
     */
    @RequestMapping(value = "/import/template")
    @RequiresPermissions("sysOffice:import")
    public ResultInfo importFileTemplate(HttpServletResponse response) {
        try {
            String fileName = "机构信息数据导入模板.xlsx";
            List<SysOffice> list = new ArrayList<>();
            new ExportExcel("机构信息数据", SysOffice.class, 1).setDataList(list).write(response, fileName).dispose();
            return ResultInfo.ok("下载模板成功！");
        } catch (Exception e) {
            return ResultInfo.error("导入模板下载失败！失败信息：" + e.getMessage());
        }
    }


}



